//
//  AccountDownloadCell.m
//  ImGuider
//
//  Created by llt on 2017/6/7.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AccountDownloadCell.h"

@interface AccountDownloadCell ()

@property (weak, nonatomic) IBOutlet UIImageView *downloadImageView;

@property (weak, nonatomic) IBOutlet UILabel *downloadNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *totoalSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *downloadStateLabel;
@property (weak, nonatomic) IBOutlet UILabel *guiderNameLabel;

@end

@implementation AccountDownloadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDownload) name:MCDownloadStartNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDownload) name:MCDownloadStopNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDownload) name:MCDownloadFinishNotification object:nil];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setLineModel:(LineModel *)lineModel {
    
    _lineModel = lineModel;
    
    [self.downloadImageView setImageURL:[NSURL URLWithString:lineModel.pictures] placeholderImage:[UIImage imageNamed:@"placeholder_squ"]];
    self.downloadNameLabel.text = lineModel.linename;
    self.guiderNameLabel.text = lineModel.guide.realname;
//    self.timeLabel.text = [NSString stringWithFormat:@"%@min",lineModel.timelabel.length > 0 ? lineModel.timelabel : @"--"];
    
//    self.scenicCountLabel.text = lineModel.recordcount;
    
//    self.scenicCountLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"MYDOWNLOAD_TOTAL", nil),lineModel.recordcount];
    [self reloadDownload];
}
- (IBAction)deleteAction:(id)sender {
    
    if ([_delegate respondsToSelector:@selector(deleteActionWithModel:)]) {
        
        [_delegate deleteActionWithModel:self.lineModel];
    }
}

- (void)reloadDownload {

    __block LineModel *lineModel = self.lineModel;
    
    [[DownloadManager sharedManager] downloadStatusForModel:lineModel block:^(MCDownloadState state, NSInteger completeCount, NSString *unionid) {
        
        if (![lineModel.unionid isEqualToString:unionid]) {
            return ;
        }
        
        if (self.lineModel.type.integerValue == 4) {
            
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
                long long bytes = 0;
                
                for (ScenicPoint *point in lineModel.records) {
                    
                    bytes += [[DownloadManager sharedManager] fileSizeForURLString:point.playpath];
                }
                
                dispatch_async(dispatch_get_main_queue(), ^ {
                    self.totoalSizeLabel.text = [IGUtil changeFileSize:bytes];
                });
            });
            
            self.downloadStateLabel.text = [NSString stringWithFormat:@"(%ld/%ld)",completeCount,self.lineModel.records.count];
            
            return;
        }
        
        
        switch (state) {
            case MCDownloadStateFailed: {
                
                self.totoalSizeLabel.text = @"";
                self.downloadStateLabel.text = MYLocalizedString(@"MYDOWNLOAD_STATE_FAILED", nil);
            }
                break;
            case MCDownloadStateCompleted: {
                
                self.downloadStateLabel.text = [NSString stringWithFormat:@"(%@)",MYLocalizedString(@"MYDOWNLOAD_STATE_SUCCESS", nil)];
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    
                    long long bytes = 0;
                    
                    for (ScenicPoint *point in lineModel.records) {
                        
                        bytes += [[DownloadManager sharedManager] fileSizeForURLString:point.playpath];
                    }
                    
                    dispatch_async(dispatch_get_main_queue(), ^ {
                        self.totoalSizeLabel.text = [IGUtil changeFileSize:bytes];
                    });
                });
            }
                break;
            case MCDownloadStateSuspened: {
                
                self.totoalSizeLabel.text = @"";
                self.downloadStateLabel.text = MYLocalizedString(@"MYDOWNLOAD_STATE_SUSPEND", nil);
            }
                break;
                
            default: {
                
                self.totoalSizeLabel.text = @"";
                self.downloadStateLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"MYDOWNLOAD_STATE_DOWNLOADING", nil),completeCount * 1.0 / lineModel.records.count *100];
            }
                break;
        }
    }];
}

@end

